#include	"Triangle.h"
#include	<windows.h>
#include	<gl/gl.h>

Triangle::Triangle()
	:	m_material(0xFFFFFFFF)
	,	m_normal(0.0f, 0.0f, 0.0f)
	,	m_center(0.0f, 0.0f, 0.0f)
{
}

Triangle::Triangle(unsigned int material, const Vector3f vertex[3], const Vector2f texture[3])
	:	m_material(material)
	,	m_normal(0.0f, 0.0f, 0.0f)
	,	m_center(0.0f, 0.0f, 0.0f)
{
	for(unsigned int i = 0; i < 3; ++i)
	{
		m_vertex[i] = vertex[i];
		m_textureVertex[i] = texture[i];
	}
	ComputeCenter();
	ComputeNormal();
}

Triangle::~Triangle()
{
}

void Triangle::Set(unsigned int material, const Vector3f vertex[3], const Vector2f texture[3])
{
	m_material = material;
	for(unsigned int i = 0; i < 3; ++i)
	{
		m_vertex[i] = vertex[i];
		m_textureVertex[i] = texture[i];
	}
	ComputeCenter();
	ComputeNormal();
}

unsigned int Triangle::Material() const
{
	return m_material;
}

void Triangle::Material(unsigned int material)
{
	m_material = material;
}

const Vector3f * Triangle::Vertex() const
{
	return m_vertex;
}

Vector3f * Triangle::Vertex()
{
	return m_vertex;
}

const Vector2f * Triangle::Texture() const
{
	return m_textureVertex;
}

Vector2f * Triangle::Texture()
{
	return m_textureVertex;
}

void Triangle::Render() const
{
	for(unsigned int i = 0; i < 3; ++i)
	{
		glTexCoord2fv(m_textureVertex[i]);
		glVertex3fv(m_vertex[i]);
	}
}

void Triangle::RenderNormal() const
{
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex3fv(m_center);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex3fv(m_center + m_normal);
}

const Vector3f & Triangle::Normal() const
{
	return m_normal;
}

const Vector3f & Triangle::ComputeNormal()
{
	Vector3f v1(m_vertex[0] - m_vertex[1]);
	Vector3f v2(m_vertex[0] - m_vertex[2]);
	m_normal = CrossProduct(v1, v2);
	m_normal.Normalize();
	return m_normal;
}

const Vector3f & Triangle::Center() const
{
	return m_center;
}

const Vector3f & Triangle::ComputeCenter()
{
	m_center = (m_vertex[0] + m_vertex[1] + m_vertex[2]) / 3;
	return m_center;
}

